package com.bolang.onlycode.demo.socket;

import java.io.*;
import java.net.Socket;

/**
 * @Auther: bl_xueld
 * @Date: 2021/12/14 16:24
 * @Description:
 */
public class MySocket {
    public static void main(String[] args) {
        Socket socket=null;
        try {
           socket = new Socket("127.0.0.1", 8080);
            OutputStream os = socket.getOutputStream();
            Boolean autoflush=true;
            PrintWriter out = new PrintWriter(socket.getOutputStream(), autoflush);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out.println("Get /index.jsp HTTP/1.1");
            out.println("Host: 1ocalhost:8080");
            out.println("Connection: Close");
            out.println();
            boolean loop=true;
            StringBuffer sb = new StringBuffer(8096);
            while (loop){
                if(in.ready()){
                    int i=0;
                    while (i!=-1){
                        i=in.read();
                        sb.append((char)i);
                    }
                    loop=false;
                }
                Thread.sleep(50);
            }
            System.out.println(sb.toString());



        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



    }

}
